package com.gticket.orderservice.mapper;

import com.gticket.orderservice.dto.TimeOrder;
import com.gticket.orderservice.pojo.OrderItems;
import com.gticket.orderservice.pojo.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: 伴你时光尽头
 * @Date: 2019/11/18 16:14
 */
@Repository
public interface OrderMapper {

    /**
     * 查询整个影院所有的订单信息
     *
     * @param orders 包含影院的数据库特有字段信息的order对象
     * @return
     */
    @Select("select * from ${databasename}.orders")
    public List<Orders> getAll(Orders orders);


    /**
     * 创建订单
     * 创建订单  此时只需要插入 订单号 用户id  影院id
     *
     * @param orders
     */
    @Insert("insert into ${databasename}.orders(order_num,user_id,cinema_id) values(#{order_num},#{user_id},#{cinema_id})")
    public void addOrder(Orders orders);

    /**
     * 改变订单状态为  2  已失效
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set order_state = 2 where order_num = #{order_num}")
    public void updateState(Orders orders);


    /**
     * 改变订单状态为  1  已支付
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set order_state = 1 where order_num = #{order_num}")
    public void updateStateToAlreadyPay(Orders orders);

    /**
     * 通过订单编号查询订单id
     *
     * @param orders
     * @return
     */
    @Select("select order_id from ${databasename}.orders where order_num = #{order_num}")
    public Integer selectOrder_idByOrderNum(Orders orders);

    /**
     * 查询订单状态  返回对应的额状态值
     *
     * @param orders
     * @return
     */
    @Select("select order_state from ${databasename}.orders where order_num = #{order_num}")
    public int getState(Orders orders);

    /**
     * 根据订单编号id查询对应orders表中所有信息
     *
     * @param orders orders对象中封装信息为数据库前缀名和订单编号id
     * @return
     */
    @Select("select * from ${databasename}.orders where order_id = #{order_id}")
    public Orders getOrderById(Orders orders);

    /**
     * 根据订单编号id查询对应orders表中所有信息
     *
     * @param orders orders对象中封装信息为数据库前缀名和订单编号id
     * @return
     */
    @Select("select * from ${databasename}.orders where user_id = #{user_id}")
    public List<Orders> getOrderByUser_id(Orders orders);

    /**
     * 根据cinema_id查询cinema_name
     *
     * @param orders
     * @return
     */
    @Select("select cinema_name from ${databasename}.cinema where cinema_id = #{cinema_id}")
    public String getCinema_nameByCinema_id(Orders orders);


    /**
     * 根据订单创建的时间段查询对应orders表中所有信息
     *
     * @param timeOrder orders对象中封装信息为数据库前缀名和订单编号id
     * @return
     */
    @Select("select * from ${databasename}.orders where create_time > #{create_time} and create_time < #{lastTime}")
    public List<Orders> getOrderBYCreate_time(TimeOrder timeOrder);

    /**
     * 根据订单编号修改订单状态为已取消  5
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set order_state=5 where order_id=#{order_id}")
    public void changeOrderStateByOrder_id(Orders orders);

    /**
     * 通过order_num和databasename查询对应的数据行
     *
     * @param orders
     * @return
     */
    @Select("select * from ${databasename}.orders where order_num=#{order_num}")
    public Orders getOrdersByOrder_num(Orders orders);


    /**
     * 通过order_num和databasename在对应的order中存入交易号
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set pay_num=#{pay_num} where order_num=#{order_num}")
    public void setPay_num(Orders orders);

    /**
     * 通过order_num和databasename在对应的order中存入    取票码
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set ticketnum=#{pay_num} where order_num=#{order_num}")
    public void setTicketnum(Orders orders);

    /**
     * 改变订单状态状态为  5 已取消s
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set order_state=5 where order_id=#{order_id}")
    public void updateStateToAlreadyCancel(Orders orders);

    /**
     * 已经成功的订单  取消   删除取票码
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set ticketnum=null where order_id=#{order_id}")
    public void deleteTicketnum(Orders orders);

    @Update("update ${databasename}.orders set totalprice=#{totalprice} where order_id=#{order_id}")
    public void setTotalPrice(Orders order);


    /**
     * 用于已购票未使用 但是电影已经播放完成   此时自动修改电影订单为已完成或已使用状态
     *
     * @param orderItems
     */

    @Update("update ${databasename}.orders set order_state=4 where order_state = 1 and order_id=(select order_id from ${databasename}.orderitems where fs_id=#{fs_id})")
    public void updateStateComplete(OrderItems orderItems);


    /**
     * 根据取票码取票时   将对应的订单状态更改为已取票
     *
     * @param orders
     */
    @Update("update ${databasename}.orders set order_state = 3 where ticketnum=#{ticketnum}")
    public void updataStateToAlreadyTake(Orders orders);


    /**
     * 根据ticketnum和databasename获取到完整的订单信息
     *
     * @param orders
     * @return
     */
    @Select("select * from ${databasename}.orders where ticketnum=#{ticketnum}")
    public Orders getOrdersByTicketnum(Orders orders);

}
